package com.sangmo.fts.training.service;

import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.training.model.Training;
import com.sangmo.fts.training.repo.TrainingRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;


@Service
@Transactional
public class TrainingEntityService extends AuthorityEntityService<Training> {

    @Autowired
    private TrainingRepo trainingRepo;

    @Override
    protected IDataViewRepo<Training> getRepo() {
        return trainingRepo;
    }


    protected Training findPendingTraining(String businessType, String mode, Long userId) {
        return trainingRepo.findFirstByCreatedByAndBusinessTypeAndModeAndExaminationIdIsNull(userId, businessType, mode);
    }

    protected Training findTeamTraining(Long teamId, String businessType) {
        return trainingRepo.findFirstByTeamIdAndBusinessTypeAndExaminationIdIsNull(teamId, businessType);
    }

    protected Training findByExaminationIdAndUserId(Long examinationId, Long userId) {
        return trainingRepo.findFirstByExaminationIdAndCreatedBy(examinationId, userId);
    }

    protected Training findByExaminationIdAndTeamId(Long examinationId, Long teamId) {
        return trainingRepo.findFirstByExaminationIdAndTeamId(examinationId, teamId);
    }

    protected Collection<Training> findByExaminationId(Long examinationId) {
        return trainingRepo.findByExaminationId(examinationId);
    }
}
